package com.zretc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zretc.dao.ManagerMapper;
import com.zretc.entity.Manager;
import com.zretc.service.ManagerLoginService;
import com.zretc.util.MD5Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author 何亮龙
 * 管理员操作服务层实现类
 */
@Service
public class ManagerLoginServiceImpl implements ManagerLoginService {
    private static final Logger logger = LoggerFactory.getLogger(ManagerLoginServiceImpl.class);
    @Autowired
    private ManagerMapper managerMapper;


    @Override
    public Manager adminLogin(String adminName, String adminPwd) {

        return managerMapper.findManagerByAdminNameAndPwd(adminName, adminPwd);
    }

    @Override
    public boolean existsOfAdminName(String adminName) {
        Manager existsOfAdminName = managerMapper.findManagerByAdminName(adminName);
        return existsOfAdminName != null ? true : false;
    }

    @Override
    public boolean addManager(Manager manager) {

        String adminPwd = manager.getAdminPwd();
        //进行MD5加密
        MD5Util md5 = new MD5Util();
        adminPwd = md5.MD5(adminPwd);
        Integer adminId = manager.getAdminId();
        String adminName = manager.getAdminName();
        String adminRole = manager.getAdminRole();
        String adminRegTime = manager.getAdminRegTime();
        Manager manager1 = new Manager(adminId,adminName,adminPwd,adminRole,adminRegTime);
        int i = managerMapper.insertManager(manager1);
        if(i>0){
            return true;
        }else{

        }
        return false;
    }

    @Override
    public boolean deleteByAdminId(Integer accountId) {
        int n = managerMapper.deleteManagerByAdminId(accountId);
        if (n > 0) {
            logger.info("====================================单条管理员信息删除成功" );
            return true;
        } else {
            logger.info("========================单条管理员信息失败===========================================" );
            return false;
        }
    }


    @Override
    public PageInfo<Manager> getManagerByCondition(Map<String, String> condition) {
        // 获取页码
        String num = condition.get("pageNum");
        if (num == null) {
            num = "1";
        }
        Integer pageNum = Integer.valueOf(num);
        // 获取显示数量
        String size = condition.get("pageSize");
        if (size == null) {
            size = "10";
        }
        //管理员ID
        String admintId = condition.get("accountId");
        //管理员名称
        String adminName = condition.get("accountName");
        // 管理员角色
        String adminRole = condition.get("adminRole");

        Map<String, String> map = new HashMap<>();
        if ("".equals(admintId) || admintId == null) {
            map.put("admintId",null);
        } else {
            map.put("admintId",admintId);
        }
        if ("".equals(adminName) || adminName == null) {
            map.put("adminName",null);
        } else {
            map.put("adminName","%" + adminName + "%");
        }
        if ("".equals(adminRole) || adminRole == null) {
            map.put("adminRole",null);
        } else {
            map.put("adminRole",adminRole);
        }

        Integer pageSize = Integer.valueOf(size);
        PageHelper.startPage(pageNum, pageSize);
        List<Manager> managersList = managerMapper.findManagerByCondition(map);
        PageInfo<Manager> pageInfo = new PageInfo<Manager>(managersList);
        return pageInfo;
    }
}
